package com.gdpu.liou.symphonyserver.common.jobhandler;

import com.alibaba.fastjson.JSON;
import com.gdpu.liou.symphonyserver.entity.Song;
import com.gdpu.liou.symphonyserver.service.SongService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author liou
 * @date 2022/2/28 11:56 周一
 */
@Slf4j
@Service
public class XxlJobService {

    @Autowired
    private SongService songService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    /**
     * 每日00：00，更新随机歌曲列表添加进redis缓存
     */
    @XxlJob("test")
    public void testJobHandle(){
        log.info("=====>启动定时任务：testJobHandle");
        log.info("=====>定时任务执行完成：updateRandomSongList，将随机歌曲列表添加进redis缓存");
    }

    /**
     * test
     */
    @XxlJob("testJobHandler")
    public void testJobHandler(){
        log.info("=====>启动定时任务：testJobHandler");

        log.info("=====>定时任务执行完成：testJobHandler");
    }


    /**
     * 每日00：00，更新随机歌曲列表添加进redis缓存
     */
    @XxlJob("updateRandomSongList")
    public void updateRandomSongListJobHandle(){
        log.info("=====>启动定时任务：updateRandomSongList");
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        List<Song> songs = null;
        songs = songService.randomSong();
        // 添加Redis缓存有效期一天
        String songsJson = JSON.toJSONString(songs);
        operations.set("randomSong", songsJson, 1, TimeUnit.DAYS);
        log.info("=====>定时任务执行完成：updateRandomSongList，将随机歌曲列表添加进redis缓存");
    }
}
